package com.gengzp.system.controller;

import com.gengzp.common.constants.PermissionConstants;
import com.gengzp.common.document.annotations.Api;
import com.gengzp.common.document.annotations.ApiOperation;
import com.gengzp.common.model.base.req.SearchByIdReq;
import com.gengzp.common.model.base.resp.Result;
import com.gengzp.common.model.pagination.resp.PaginationResult;
import com.gengzp.common.model.user.req.EditSysUserReq;
import com.gengzp.common.model.user.req.PageSysUserReq;
import com.gengzp.common.model.user.resp.PageSysUserResp;
import com.gengzp.common.model.user.resp.SearchSysUserResp;
import com.gengzp.permission.annotation.CheckPermission;
import com.gengzp.permission.constants.PermissionExceptionConstants;
import com.gengzp.permission.exception.PermissionException;
import com.gengzp.permission.utils.PermissionUtils;
import com.gengzp.system.service.ISysUserService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @ClassName SysUserController
 * @Description 系统用户管理模块控制层
 * @Author gengzp
 * @Date 2025/5/16 0:06
 */
@RestController
@RequestMapping("/user")
@Api(tags = "系统用户管理模块")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 编辑系统用户信息
     */
    @PostMapping("/edit")
    @ApiOperation(value = "编辑系统用户信息")
    public Result<Void> editSysUser(@Valid @RequestBody EditSysUserReq req) {
        // 权限校验: 普通用户只能修改自己的信息, 有修改用户权限才能修改其他用户的信息
        if (!PermissionUtils.verifyUserIsSuperAdmin(PermissionUtils.getCurrentUserId()) &&
                !Objects.equals(req.getUserId(), PermissionUtils.getCurrentUserId()) &&
                !PermissionUtils.getCurrentUserOwnedPermissionCodes().contains(PermissionConstants.USER_WRITE)) {
            throw PermissionException.get(PermissionExceptionConstants.NO_AUTHENTICATED, "没有权限修改当前用户的信息");
        }

        // 修改的是超级管理员的信息, 那么直接提示超级管理员信息无法修改
        if (Objects.equals(PermissionUtils.getSuperAdminUserId(), req.getUserId())) {
            throw PermissionException.get(PermissionExceptionConstants.NO_AUTHENTICATED, "超级管理员信息无法修改");
        }

        sysUserService.editSysUser(req);
        return Result.getSuccessResult();
    }

    /**
     * 删除用户信息
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除用户信息")
    @CheckPermission(PermissionConstants.USER_WRITE)
    public Result<Void> removeSysUserById(@Valid @RequestBody SearchByIdReq req) {
        sysUserService.removeSysUserById(Long.valueOf(req.getId()));
        return Result.getSuccessResult();
    }

    /**
     * 分页查询系统用户信息
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询系统用户信息")
    @CheckPermission(PermissionConstants.USER_READ)
    public Result<PaginationResult<PageSysUserResp>> pageQuerySysUser(@Valid @RequestBody PageSysUserReq req) {
        return Result.getSuccessResult(sysUserService.pageQuerySysUser(req));
    }

    /**
     * 根据id查询系统用户信息
     */
    @PostMapping("/search_by_id")
    @ApiOperation(value = "根据id查询系统用户信息")
    public Result<SearchSysUserResp> searchSysUserById(@Valid @RequestBody SearchByIdReq req) {
        return Result.getSuccessResult(sysUserService.searchSysUserById(Long.valueOf(req.getId())));
    }

}
